package com.ruoyi.plus.pub.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.api.BaseApi;
import com.ruoyi.common.core.api.version.ApiVersion;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.plus.constants.PlusDictConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.plus.pub.api.po.PubOpinionPo;
import com.ruoyi.plus.pub.service.IPubOpinionService;
import com.ruoyi.plus.pub.domain.PubOpinion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 意见反馈Api
 *
 */
@Slf4j
@Api(tags = "PubOpinionApi-意见反馈")
@ApiVersion()
@RestController
@RequestMapping("app/api/pub/pubOpinionApi")
public class PubOpinionApi extends BaseApi {

    @Autowired
    private IPubOpinionService pubOpinionService;

    /**
     * 新增保存意见反馈
     */
    @ApiOperation("新增保存意见反馈")
    @Log(title = "add-意见反馈", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public ApiResult<Boolean> addSave(@Validated @RequestBody PubOpinionPo pubOpinionPo) {
        if (StringUtils.isEmpty(pubOpinionPo.getContent())) {
            log.error("意见反馈未传入内容");
            return new ApiResult<Boolean>(getApiVersion()).setCode(-1).setData(false).setMsg("意见反馈未传入内容").setApiVersion(this.getApiVersion());
        }
        PubOpinion pubOpinion = new PubOpinion();
        BeanUtils.copyProperties(pubOpinionPo, pubOpinion);
        pubOpinion.setStatus(PlusDictConstants.SYS_OPINION_UN_READ);
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        pubOpinion.setUserId(sysUser.getUserId());
        pubOpinion.setUserName(sysUser.getNickName());
        boolean flag = pubOpinionService.save(pubOpinion);
        return new ApiResult<Boolean>(getApiVersion()).setData(flag).setApiVersion(this.getApiVersion());
    }


}
